﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Hongapo_Common;

namespace Hongapo_Services.Mall
{
    public class Mall
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Mall");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary> 
        public int Add(Hongapo_Model.Mall.Mall model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Mall(");
            strSql.Append("MallNo,MallType,MallName,Title,Logo,Url,Sort,StarLevel,RatingClicks,MallDesc,MallInfo,Isopen,IsAudit,SiteTel,Location,Services,CreateUser,CreateDate,Clicks,Recomens,Criticis,Remark)");
            strSql.Append(" values (");
            strSql.Append("@MallNo,@MallType,@MallName,@Title,@Logo,@Url,@Sort,@StarLevel,@RatingClicks,@MallDesc,@MallInfo,@Isopen,@IsAudit,@SiteTel,@Location,@Services,@CreateUser,@CreateDate,@Clicks,@Recomens,@Criticis,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MallNo", SqlDbType.VarChar,20),
					new SqlParameter("@MallType", SqlDbType.VarChar,50),
					new SqlParameter("@MallName", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@Logo", SqlDbType.VarChar,200),
					new SqlParameter("@Url", SqlDbType.VarChar,100),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@StarLevel", SqlDbType.Int,4),
					new SqlParameter("@RatingClicks", SqlDbType.Int,4),
					new SqlParameter("@MallDesc", SqlDbType.NVarChar,500),
					new SqlParameter("@MallInfo", SqlDbType.Text),
					new SqlParameter("@Isopen", SqlDbType.Int,4),
					new SqlParameter("@IsAudit", SqlDbType.Int,4),
					new SqlParameter("@SiteTel", SqlDbType.VarChar,50),
					new SqlParameter("@Location", SqlDbType.NVarChar,50),
					new SqlParameter("@Services", SqlDbType.NVarChar,200),
					new SqlParameter("@CreateUser", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Clicks", SqlDbType.Int,4),
					new SqlParameter("@Recomens", SqlDbType.Int,4),
					new SqlParameter("@Criticis", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.MallNo;
            parameters[1].Value = model.MallType;
            parameters[2].Value = model.MallName;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.Logo;
            parameters[5].Value = model.Url;
            parameters[6].Value = model.Sort;
            parameters[7].Value = model.StarLevel;
            parameters[8].Value = model.RatingClicks;
            parameters[9].Value = model.MallDesc;
            parameters[10].Value = model.MallInfo;
            parameters[11].Value = model.Isopen;
            parameters[12].Value = model.IsAudit;
            parameters[13].Value = model.SiteTel;
            parameters[14].Value = model.Location;
            parameters[15].Value = model.Services;
            parameters[16].Value = model.CreateUser;
            parameters[17].Value = model.CreateDate;
            parameters[18].Value = model.Clicks;
            parameters[19].Value = model.Recomens;
            parameters[20].Value = model.Criticis;
            parameters[21].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Hongapo_Model.Mall.Mall model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Mall set ");
            strSql.Append("MallNo=@MallNo,");
            strSql.Append("MallType=@MallType,");
            strSql.Append("MallName=@MallName,");
            strSql.Append("Title=@Title,");
            strSql.Append("Logo=@Logo,");
            strSql.Append("Url=@Url,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("StarLevel=@StarLevel,");
            strSql.Append("RatingClicks=@RatingClicks,");
            strSql.Append("MallDesc=@MallDesc,");
            strSql.Append("MallInfo=@MallInfo,");
            strSql.Append("Isopen=@Isopen,");
            strSql.Append("IsAudit=@IsAudit,");
            strSql.Append("SiteTel=@SiteTel,");
            strSql.Append("Location=@Location,");
            strSql.Append("Services=@Services,");
            strSql.Append("CreateUser=@CreateUser,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("Clicks=@Clicks,");
            strSql.Append("Recomens=@Recomens,");
            strSql.Append("Criticis=@Criticis,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@MallNo", SqlDbType.VarChar,20),
					new SqlParameter("@MallType", SqlDbType.VarChar,50),
					new SqlParameter("@MallName", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@Logo", SqlDbType.VarChar,200),
					new SqlParameter("@Url", SqlDbType.VarChar,100),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@StarLevel", SqlDbType.Int,4),
					new SqlParameter("@RatingClicks", SqlDbType.Int,4),
					new SqlParameter("@MallDesc", SqlDbType.NVarChar,500),
					new SqlParameter("@MallInfo", SqlDbType.Text),
					new SqlParameter("@Isopen", SqlDbType.Int,4),
					new SqlParameter("@IsAudit", SqlDbType.Int,4),
					new SqlParameter("@SiteTel", SqlDbType.VarChar,50),
					new SqlParameter("@Location", SqlDbType.NVarChar,50),
					new SqlParameter("@Services", SqlDbType.NVarChar,200),
					new SqlParameter("@CreateUser", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Clicks", SqlDbType.Int,4),
					new SqlParameter("@Recomens", SqlDbType.Int,4),
					new SqlParameter("@Criticis", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,200),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.MallNo;
            parameters[1].Value = model.MallType;
            parameters[2].Value = model.MallName;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.Logo;
            parameters[5].Value = model.Url;
            parameters[6].Value = model.Sort;
            parameters[7].Value = model.StarLevel;
            parameters[8].Value = model.RatingClicks;
            parameters[9].Value = model.MallDesc;
            parameters[10].Value = model.MallInfo;
            parameters[11].Value = model.Isopen;
            parameters[12].Value = model.IsAudit;
            parameters[13].Value = model.SiteTel;
            parameters[14].Value = model.Location;
            parameters[15].Value = model.Services;
            parameters[16].Value = model.CreateUser;
            parameters[17].Value = model.CreateDate;
            parameters[18].Value = model.Clicks;
            parameters[19].Value = model.Recomens;
            parameters[20].Value = model.Criticis;
            parameters[21].Value = model.Remark;
            parameters[22].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Mall ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Mall ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Hongapo_Model.Mall.Mall GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,MallNo,MallType,MallName,Title,Logo,Url,Sort,StarLevel,RatingClicks,MallDesc,MallInfo,Isopen,IsAudit,SiteTel,Location,Services,CreateUser,CreateDate,Clicks,Recomens,Criticis,Remark from T_Mall ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            Hongapo_Model.Mall.Mall model = new Hongapo_Model.Mall.Mall();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MallNo"] != null && ds.Tables[0].Rows[0]["MallNo"].ToString() != "")
                {
                    model.MallNo = ds.Tables[0].Rows[0]["MallNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MallType"] != null && ds.Tables[0].Rows[0]["MallType"].ToString() != "")
                {
                    model.MallType = ds.Tables[0].Rows[0]["MallType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MallName"] != null && ds.Tables[0].Rows[0]["MallName"].ToString() != "")
                {
                    model.MallName = ds.Tables[0].Rows[0]["MallName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Title"] != null && ds.Tables[0].Rows[0]["Title"].ToString() != "")
                {
                    model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Logo"] != null && ds.Tables[0].Rows[0]["Logo"].ToString() != "")
                {
                    model.Logo = ds.Tables[0].Rows[0]["Logo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Url"] != null && ds.Tables[0].Rows[0]["Url"].ToString() != "")
                {
                    model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Sort"] != null && ds.Tables[0].Rows[0]["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StarLevel"] != null && ds.Tables[0].Rows[0]["StarLevel"].ToString() != "")
                {
                    model.StarLevel = int.Parse(ds.Tables[0].Rows[0]["StarLevel"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RatingClicks"] != null && ds.Tables[0].Rows[0]["RatingClicks"].ToString() != "")
                {
                    model.RatingClicks = int.Parse(ds.Tables[0].Rows[0]["RatingClicks"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MallDesc"] != null && ds.Tables[0].Rows[0]["MallDesc"].ToString() != "")
                {
                    model.MallDesc = ds.Tables[0].Rows[0]["MallDesc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MallInfo"] != null && ds.Tables[0].Rows[0]["MallInfo"].ToString() != "")
                {
                    model.MallInfo = ds.Tables[0].Rows[0]["MallInfo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Isopen"] != null && ds.Tables[0].Rows[0]["Isopen"].ToString() != "")
                {
                    model.Isopen = int.Parse(ds.Tables[0].Rows[0]["Isopen"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsAudit"] != null && ds.Tables[0].Rows[0]["IsAudit"].ToString() != "")
                {
                    model.IsAudit = int.Parse(ds.Tables[0].Rows[0]["IsAudit"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SiteTel"] != null && ds.Tables[0].Rows[0]["SiteTel"].ToString() != "")
                {
                    model.SiteTel = ds.Tables[0].Rows[0]["SiteTel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Location"] != null && ds.Tables[0].Rows[0]["Location"].ToString() != "")
                {
                    model.Location = ds.Tables[0].Rows[0]["Location"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Services"] != null && ds.Tables[0].Rows[0]["Services"].ToString() != "")
                {
                    model.Services = ds.Tables[0].Rows[0]["Services"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CreateUser"] != null && ds.Tables[0].Rows[0]["CreateUser"].ToString() != "")
                {
                    model.CreateUser = int.Parse(ds.Tables[0].Rows[0]["CreateUser"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateDate"] != null && ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Clicks"] != null && ds.Tables[0].Rows[0]["Clicks"].ToString() != "")
                {
                    model.Clicks = int.Parse(ds.Tables[0].Rows[0]["Clicks"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Recomens"] != null && ds.Tables[0].Rows[0]["Recomens"].ToString() != "")
                {
                    model.Recomens = int.Parse(ds.Tables[0].Rows[0]["Recomens"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Criticis"] != null && ds.Tables[0].Rows[0]["Criticis"].ToString() != "")
                {
                    model.Criticis = int.Parse(ds.Tables[0].Rows[0]["Criticis"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,MallNo,MallType,MallName,Title,Logo,Url,Sort,StarLevel,RatingClicks,MallDesc,MallInfo,Isopen,IsAudit,SiteTel,Location,Services,CreateUser,CreateDate,Clicks,Recomens,Criticis,Remark ");
            strSql.Append(" FROM T_Mall ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,MallNo,MallType,MallName,Title,Logo,Url,Sort,StarLevel,RatingClicks,MallDesc,MallInfo,Isopen,IsAudit,SiteTel,Location,Services,CreateUser,CreateDate,Clicks,Recomens,Criticis,Remark ");
            strSql.Append(" FROM T_Mall ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

  
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            parameters[2].Value = strWhere;
            return DbHelperSQL.RunProcedure("P_MallGetListByPager", parameters, "ds");
        }

        #endregion  Method
    }
}
